package com.intelligencesoft.pss.service.auth;

import java.util.Properties;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.intelligencesoft.pss.auth.LoginUtil;
import com.intelligencesoft.pss.dao.auth.AuthDao;
import com.intelligencesoft.pss.entity.base.UserInfo;
import com.intelligencesoft.pss.realm.auth.CustomUsernamePasswordToken;
import com.intelligencesoft.pss.utils.auth.AuthEncryption;


/**
 * 登录认证业务逻辑层
 * @author Administrator
 *
 */
@Service
public class AuthService {

	@Autowired
	private AuthDao authDao;
	@Autowired
	private AuthEncryption authEncryption;
	@Autowired
	@Qualifier("authConfig")
	private Properties properties;

	@Autowired
	private LoginUtil loginUtil;
	/**
	 * 用户注册入口
	 * @param userinfo
	 * @return
	 */
	public String saveUserinfo(UserInfo userinfo) {
		String flag = "error";
		try {
			userinfo.setPassword(authEncryption.md5Hash(userinfo.getPassword()));
			authDao.getHibernateTemplate().saveOrUpdate(userinfo);
			flag = "success";
		} catch (DataAccessException e) {
			flag = "error";
		}
		return flag;
	}

	/**
	 * 用户登录入口
	 * @param userinfo
	 * @return
	 */
	public UserInfo login(UserInfo userinfo)throws IncorrectCredentialsException,AuthenticationException,Exception {
		Subject currentUser = SecurityUtils.getSubject();
		if (!currentUser.isAuthenticated()) {
			CustomUsernamePasswordToken token = new CustomUsernamePasswordToken(
					userinfo.getUserName(), userinfo.getPassword());
			currentUser.login(token);
		}
		UserInfo u = (UserInfo) currentUser.getPrincipal();
		return u;
	}

	/**
	 * 用户退出入口
	 */
	public void logout() {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
	}
	
	/**
	 * 修改口令功能入口
	 * @param userinfo
	 * @return
	 */
	public String updatePassword(UserInfo userinfo) {
		String flag="error";
		if (userinfo!=null&&null!=userinfo.getId()&&null!=userinfo.getPassword()&&!"".equals(userinfo.getPassword())) {
			authDao.getHibernateTemplate().saveOrUpdate(userinfo);
			flag="success";
		}
		return flag;
	}
	

	/**
	 * 根据登录名及密码查找用户
	 * @param loginName 登录名
	 * @param password 密码
	 * @param authType 暂时只支持3种方式：email,mobilephone,emailormobilephone
	 * @return
	 */
	public UserInfo findUserinfoByAuth(String loginName, String password) {
		UserInfo userinfo = new UserInfo();
		userinfo = authDao.findByUserNameAndPassword(new UserInfo(loginName,password));
		return userinfo;
	}

	/**
	 * 根据邮箱及密码查找用户
	 * @param email 邮箱
	 * @param password 密码
	 * @return
	 */
	public UserInfo findByEmailAndPassword(String email, String password) {
		return null;
	}
	
	
	public UserInfo findUserinfoById(String id) {
		return authDao.getHibernateTemplate().get(UserInfo.class, id);
	}
	/**
	 * 根据手机号及密码查找用户
	 * @param mobilePhone 手机号
	 * @param password 密码
	 * @return
	 */
	public UserInfo findByMobilePhoneAndPassword(String mobilePhone,
			String password) {
		return null;
	}

}
